/* ----------------------------------------------------------------------
*
* Copyright (C) 2025 XiaoFei. All rights reserved.
*
* @file:		XiaoZhi_HotResis.c
* @author:		XiaoFei
* @Date:     	26-January-2025
* @Revision: 	V1
* @Project: 	XiaoFei BSP Library
*
* -------------------------------------------------------------------- */

#include "stm32f10x.h"
#include "XiaoZhi_HotResis.h"

/* 
* @function: 	HotResis_Init
*/
void HotResis_Init(void)
{
	#ifdef HotResis_Use_ADC1
	    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);	
	#endif
	#ifdef HotResis_Use_ADC2
	    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2,ENABLE);	
	#endif
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	#if defined(HotResis_Use_ADC2)||defined(HotResis_Use_ADC1)
	    RCC_APB2PeriphClockCmd(HotResis_RCC_AO,ENABLE);	
	    RCC_ADCCLKConfig(RCC_PCLK2_Div6);
	    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
	    GPIO_InitStructure.GPIO_Pin=HotResis_PIN_AO;
	    GPIO_Init(HotResis_GPIO_AO,&GPIO_InitStructure);
	#endif
	#ifdef HotResis_Use_DO
		RCC_APB2PeriphClockCmd(HotResis_RCC_DO,ENABLE);	
	    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	    GPIO_InitStructure.GPIO_Pin=HotResis_PIN_DO;
	    GPIO_Init(HotResis_GPIO_DO,&GPIO_InitStructure);
	#endif
	#ifdef HotResis_Use_ADC1
		#ifndef BSP_USE_ADC1
		    #define BSP_USE_ADC1
		    ADC_InitTypeDef ADC_InitStructure;	
		    ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;	
		    ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
		    ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
		    ADC_InitStructure.ADC_ContinuousConvMode=DISABLE;	
		    ADC_InitStructure.ADC_ScanConvMode=DISABLE;
		    ADC_InitStructure.ADC_NbrOfChannel=1;	
		    ADC_Init(ADC1,&ADC_InitStructure);	
		    ADC_Cmd(ADC1,ENABLE);
		    ADC_ResetCalibration(ADC1);	
		    while (ADC_GetResetCalibrationStatus(ADC1)==SET);
		    ADC_StartCalibration(ADC1);
		    while (ADC_GetCalibrationStatus(ADC1)==SET);
	    #endif
	#endif
	#ifdef HotResis_Use_ADC2
		#ifndef BSP_USE_ADC2
		    #define BSP_USE_ADC2
		    ADC_InitTypeDef ADC_InitStructure;	
		    ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;	
		    ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
		    ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
		    ADC_InitStructure.ADC_ContinuousConvMode=DISABLE;	
		    ADC_InitStructure.ADC_ScanConvMode=DISABLE;
		    ADC_InitStructure.ADC_NbrOfChannel=1;	
		    ADC_Init(ADC2,&ADC_InitStructure);	
		    ADC_Cmd(ADC2,ENABLE);
		    ADC_ResetCalibration(ADC2);	
		    while (ADC_GetResetCalibrationStatus(ADC2)==SET);
		    ADC_StartCalibration(ADC2);
		    while (ADC_GetCalibrationStatus(ADC2)==SET);
	    #endif
	#endif	
}

#if defined(HotResis_Use_ADC2)||defined(HotResis_Use_ADC1)
/* 
* @function: 	HotResis_GetValue
* @return:		The ADC value from hot resistance. This value will be mapped from 0 to 3.3 volts to 0 to 4096.
*/
unsigned int HotResis_GetValue(void)
{
	#ifdef HotResis_Use_ADC1
	    static ADC_TypeDef* ADC=ADC1;
	#endif
	#ifdef HotResis_Use_ADC2
	    static ADC_TypeDef* ADC=ADC2;
	#endif
	ADC_RegularChannelConfig(ADC,HotResis_ADC_Channel,1,ADC_SampleTime_55Cycles5);
	ADC_SoftwareStartConvCmd(ADC,ENABLE);
	while(ADC_GetFlagStatus(ADC,ADC_FLAG_EOC)==RESET);
	return (unsigned int)ADC->DR;
}
#endif

#if defined(HotResis_Use_ADC2)||defined(HotResis_Use_ADC1)
/* 
* @function: 	HotResis_GetVoltage
* @return:		The Voltage value from hot resistance. This value need to be limited to 0 to 3.3 volts.
*/
float HotResis_GetVoltage(void)
{
	#ifdef HotResis_Use_ADC1
	    static ADC_TypeDef* ADC=ADC1;
	#endif
	#ifdef HotResis_Use_ADC2
	    static ADC_TypeDef* ADC=ADC2;
	#endif
	ADC_RegularChannelConfig(ADC,HotResis_ADC_Channel,1,ADC_SampleTime_55Cycles5);
	ADC_SoftwareStartConvCmd(ADC,ENABLE);
	while (ADC_GetFlagStatus(ADC,ADC_FLAG_EOC)==RESET);
	return (3.3*ADC->DR)/4096.0;
}
#endif

#ifdef HotResis_Use_DO
/* 
* @function: 	HotResis_IsTrigger
* @return:		Return 0x01 if the hot resistance is trigger.
*/
unsigned char HotResis_IsTrigger(void)
{
	if(GPIO_ReadInputDataBit(HotResis_GPIO_DO, HotResis_PIN_DO) == HotResis_Trigger)
		return 0x01;
	else
		return 0x00;
}
#endif
